package com.example.opengldemo._camera_demo;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.TextView;
import androidx.annotation.Nullable;

import com.example.opengldemo.R;
import com.example.opengldemo._camera_demo.renderer.WaterMarkFilter;

public class MyCameraDemoActivity extends Activity {

    private SurfaceView mSurfaceView;
    private TextView mTvPreview,mTvRecord;
    private boolean isPreviewOpen=false;
    private boolean isRecordOpen=false;

    private CameraCapture mCamera;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_camera_record);
        mSurfaceView= (SurfaceView) findViewById(R.id.mSurfaceView);
        mTvRecord= (TextView) findViewById(R.id.mTvRec);
        mTvPreview= (TextView) findViewById(R.id.mTvShow);

        mCamera =new CameraCapture(getApplicationContext());

        mSurfaceView.getHolder().addCallback(new SurfaceHolder.Callback() {
            @Override
            public void surfaceCreated(SurfaceHolder holder) {
                //GroupFilter filter=new GroupFilter(getResources());
                //mCamera.setRenderer(new StickFigureFilter(getResources()));
                Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.was) ;
                if(bitmap != null){
                    Log.e("WaterMarkFilter" , bitmap.hashCode()+"");
                    mCamera.setRenderer(new WaterMarkFilter().setMarkPosition(30,10,100,76).setMark(bitmap));
                }
            }

            @Override
            public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
                mCamera.setProperty(CameraCapture.KEY_PREVIEW_WIDTH,width);
                mCamera.setProperty(CameraCapture.KEY_PREVIEW_HEIGHT,height);
                mCamera.open(0);
                mCamera.setPreviewSurface(holder.getSurface());
                mCamera.startPreview();
                isPreviewOpen=true;
            }

            @Override
            public void surfaceDestroyed(SurfaceHolder holder) {
                mCamera.stopPreview();
                mCamera.close();
            }
        });
    }

    public void onClick(View view){
        switch (view.getId()){
            case R.id.mTvShow:
                isPreviewOpen=!isPreviewOpen;
                mTvPreview.setText(isPreviewOpen?"关预览":"开预览");
                if(isPreviewOpen){
                    mCamera.startPreview();
                }else{
                    mCamera.stopPreview();
                }
                break;
            default:
                break;
        }
    }

}